package leetcode.others;

import java.util.Arrays;

//2022. 将一维数组转变成二维数组
public class Construct2DArray {


    public static void main(String[] args) {
        int[] data = {1, 2, 3, 4};

        System.out.println(Arrays.toString(construct2DArray(data, 2, 2)));
    }

    public static int[][] construct2DArray(int[] original, int m, int n) {
        int length = original.length;
        if (length != m * n) {
            return new int[][]{};
        }
        int[][] res = new int[m][n];
        int index = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                res[i][j] = original[index++];
            }
        }
        return res;
    }

}
